package com.example.test0520.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.test0520.MainActivity;
import com.example.test0520.R;
import com.example.test0520.util.StringUtils;
import com.example.test0520.util.User;

import org.litepal.LitePal;

import java.util.List;

public class LoginActivity extends BaseActivity {
    private EditText etAccount;
    private EditText etPwd;
    private Button btnLogin;
    private List<User> data;

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        etAccount = findViewById(R.id.edit_login_user);
        etPwd = findViewById(R.id.edit_login_psw);
        btnLogin = findViewById(R.id.login_btn);
    }

    @Override
    protected void initData() {
//        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        btnLogin.setOnClickListener(v -> {
            data = LitePal.findAll(User.class);
            String account = etAccount.getText().toString().trim();
            String psw = etPwd.getText().toString().trim();
            login(account, psw);
        });
    }


    private void login(String account, String psw) {
        Log.d("TAG", "login: " + data.size());
        Log.d("TAG", "login: " + account + psw);
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        } else if (StringUtils.isEmpty(psw)) {
            showToast("请输入密码");
            return;
        }
        for (User i :
                data) {
            Log.d("TAG", "login: " + i.getUsername() + "=====" + i.getPassword());
            if (account.equals(i.getUsername()) && psw.equals(i.getPassword())) {
                navigateTo(MainActivity.class);
            } else {
                showToast("账号不存在或密码错误");
                etAccount.setText("");
                etPwd.setText("");
                return;
            }

        }
    }
}